package net.itbaiyang.pattern.controller;


import net.itbaiyang.pattern.component.SmsComponent;
import net.itbaiyang.pattern.controller.request.EmailRequest;
import net.itbaiyang.pattern.controller.request.NotifyRequest;
import net.itbaiyang.pattern.controller.request.SmsRequest;
//import net.itbaiyang.pattern.email.EmailService;
import net.itbaiyang.pattern.email.EmailService;
import net.itbaiyang.pattern.enums.SmsTypeEnum;
import net.itbaiyang.pattern.factory.SmsServiceFactory;
import net.itbaiyang.pattern.notification.NotificationService;
import net.itbaiyang.pattern.sms.SmsService;
import net.itbaiyang.pattern.util.JsonData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import static net.itbaiyang.pattern.enums.BizCodeEnum.SMS_TYPE;
import static net.itbaiyang.pattern.enums.SmsTypeEnum.ALIYUN_SMS;
import static net.itbaiyang.pattern.enums.SmsTypeEnum.TENCENT_SMS;

@RestController
@RequestMapping("/api/v1")
public class SendController {

    @Autowired
     private EmailService emailService;

    @Autowired
    private SmsServiceFactory smsServiceFactory;

    @Autowired
    private NotificationService notificationService;



    @PostMapping(value = "sendEmail")
    public JsonData sendEmail(@RequestBody EmailRequest request){
        return emailService.sendSimplesMail(request);
    }

    @PostMapping(value = "sendSms")
    public JsonData sendSms(@RequestBody SmsRequest request){
        SmsTypeEnum smsType = SmsTypeEnum.fromString(request.getType());

        if (smsType!=ALIYUN_SMS&&smsType!=TENCENT_SMS){
            return JsonData.buildResult(SMS_TYPE);
        }
        SmsService smsService = smsServiceFactory.getSmsService(request.getType());
        return smsService.sendSms(request);
    }

    @PostMapping(value = "notification")
    public void notification(@RequestBody NotifyRequest request){
        notificationService.pushNotification("你好！");
    }

}
